package com.ljx.recruitment.user.controller;

import com.ljx.recruitment.common.domain.vo.resp.ApiResult;
import com.ljx.recruitment.common.domain.vo.resp.PageResp;
import com.ljx.recruitment.security.annotation.ApiPermission;
import com.ljx.recruitment.security.common.PermissionConstant;
import com.ljx.recruitment.user.domain.entity.Permission;
import com.ljx.recruitment.user.domain.vo.req.PermissionPageReq;
import com.ljx.recruitment.user.domain.vo.req.PermissionSubmitReq;
import com.ljx.recruitment.user.domain.vo.req.PermissionToRoleReq;
import com.ljx.recruitment.user.domain.vo.resp.PermissionTree;
import com.ljx.recruitment.user.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-14  22:40
 */
@Api(tags = "权限接口")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @ApiPermission(permissions = {PermissionConstant.PERMISSION_QUERY})
    @ApiOperation("获取权限分页")
    @GetMapping("/page")
    public ApiResult<PageResp<Permission>> getPage(PermissionPageReq req) {
        return ApiResult.success(permissionService.getPage(req));
    }

    @ApiPermission(permissions = {PermissionConstant.PERMISSION_SUBMIT})
    @ApiOperation("创建/修改权限")
    @PostMapping("/submit")
    public ApiResult<Permission> submit(@Validated @RequestBody PermissionSubmitReq req){
        return ApiResult.success(permissionService.submit(req));
    }

    @ApiPermission(permissions = {PermissionConstant.PERMISSION_QUERY})
    @ApiOperation("获取权限模块")
    @GetMapping("/module")
    public ApiResult<List<String>> getModule() {
        return ApiResult.success(permissionService.getModule());
    }

    @ApiPermission(permissions = {PermissionConstant.PERMISSION_QUERY})
    @ApiOperation("获取权限树型数据")
    @GetMapping("/tree")
    public ApiResult<List<PermissionTree>> getTree() {
        return ApiResult.success(permissionService.getTree());
    }

    @ApiPermission(permissions = {PermissionConstant.ROLE_SUBMIT, PermissionConstant.PERMISSION_SUBMIT})
    @ApiOperation("给角色分配权限")
    @PostMapping("/toRole")
    public ApiResult<Void> allocationToRole(@Validated @RequestBody PermissionToRoleReq req){
        permissionService.allocationToRole(req);
        return ApiResult.success();
    }


}
